package com.cy.personer.biz.impl;

import com.cy.personer.bean.Detail;
import com.cy.personer.bean.UploadResult;
import com.cy.personer.biz.DetailBiz;
import com.cy.personer.ceptor.OperationLog;
import com.cy.personer.mapper.DetailMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class DetailBizImpl implements DetailBiz {
    @Resource
    private DetailMapper detailMapper;
    @Override
    public PageInfo<Detail> findItemdetails(Detail detail,String pageSize,String pageNum) {
        int i=1,j=5;
        if(pageSize!=null){
            j=Integer.parseInt(pageSize);
        }
        if(pageNum!=null){
            i=Integer.parseInt(pageNum);
        }
        Page<Detail> page=PageHelper.startPage(i,j);
        detailMapper.findItemdetails(detail);
        PageInfo<Detail> detailPageInfo =new PageInfo<Detail>(page);
        return detailPageInfo;
    }
    @OperationLog(operType="删除",methods = "删除细项" )
    @Override
    public int delDetail(int detailId) {
        return detailMapper.delDetail(detailId);
    }
    @OperationLog(operType="添加",methods = "添加细项" )
    @Override
    public int addDetail(Detail detail) {
        return detailMapper.addDetail(detail);
    }
    @OperationLog(operType="修改",methods = "修改细项" )
    @Override
    public int editDetail(Detail detail) {
        return detailMapper.editDetail(detail);
    }

    @Override
    public Integer findItemIdByItemName(String itemName) {
        return detailMapper.findItemIdByItemName(itemName);
    }
    @OperationLog(operType="添加",methods = "批量导入细项" )
    @Override
    public UploadResult addDetails(List<List<Object>> lists) {
        UploadResult uploadResult=new UploadResult();
        int errorNum=0;
        int successNum=0;
        for(int i=0;i<lists.size();i++){
            List<Object> lo = lists.get(i);
            Integer itemId=findItemIdByItemName(String.valueOf(lo.get(1))) ;
            if(itemId!=null){
                Detail detail = new Detail();
                detail.setDetailName(String.valueOf(lo.get(0)));
                detail.setItemId(itemId);
                detail.setUnit(String.valueOf(lo.get(2)));
                detail.setMin(Integer.parseInt(String.valueOf(lo.get(3))));
                detail.setMax(Integer.parseInt(String.valueOf(lo.get(4))));
                int j=addDetail(detail);
                if(j==0){
                    errorNum+=1;
                    uploadResult.getErrList().add(lo);
                }else{
                    successNum+=1;
                }
            }else{
                errorNum+=1;
                uploadResult.getErrList().add(lo);
            }
        }
        uploadResult.setErrorNum(errorNum);
        uploadResult.setSuccessNum(successNum);
        return uploadResult;
    }
}
